package com.liumh.dao;

import java.util.*;

import org.apache.ibatis.session.SqlSession;

import com.liumh.entity.Student;
import com.liumh.utils.MyBatisUtil;

public class StudentDaoImpl implements IStudentDao {

	@Override
	public List<Student> findAllStudents() {
		SqlSession session = null;
		List<Student> list = new ArrayList<Student>();
		try {
			session = MyBatisUtil.getSession();
			// 4.调用SqlSession对象session的selectList方法执行查询数据库的操作，返回映射后的结果集合
			list = session.selectList("com.liumh.dao.IStudentDao.findAllStudents");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return list;
	}

	@Override
	public Student findStudentById(int id) {
		SqlSession session = null;
		Student student = new Student();
		try {
			session = MyBatisUtil.getSession();
			student = session.selectOne("com.liumh.dao.IStudentDao.findStudentById", id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return student;
	}

	@Override
	public int insertStudent(Student student) {
		SqlSession session = null;
		int count = 0;
		try {
			session = MyBatisUtil.getSession();
			count = session.insert("com.liumh.dao.IStudentDao.insertStudent", student);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return count;
	}

	@Override
	public int insertStudentAutoIncrement(Student student) {
		SqlSession session = null;
		int count = 0;
		try {
			session = MyBatisUtil.getSession();
			count = session.insert("com.liumh.dao.IStudentDao.insertStudentAutoIncrement", student);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return count;
	}

	@Override
	public int deleteStudentById(int id) {
		SqlSession session = null;
		int count = 0;
		try {
			session = MyBatisUtil.getSession();
			count = session.delete("com.liumh.dao.IStudentDao.deleteStudentById", id);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return count;
	}

	@Override
	public int updateStudent(Student student) {
		SqlSession session = null;
		int count = 0;
		try {
			session = MyBatisUtil.getSession();
			count = session.update("com.liumh.dao.IStudentDao.updateStudent", student);
			session.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return count;

	}

	// 按姓氏模糊查询学生
	@Override
	public List<Student> findStudentsByName(String name) {
		SqlSession session = null;
		List<Student> list = new ArrayList<Student>();
		try {
			session = MyBatisUtil.getSession();
			list = session.selectList("com.liumh.dao.IStudentDao.findStudentsByName", name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 动态查询if标签
	@Override
	public List<Student> searchStudentsIf(Student student) {
		SqlSession session = null;
		List<Student> list = new ArrayList<Student>();
		try {
			session = MyBatisUtil.getSession();
			list = session.selectList("com.liumh.dao.IStudentDao.searchStudentsIf", student);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 动态查询where标签
	@Override
	public List<Student> searchStudentsWhere(Student student) {
		SqlSession session = null;
		List<Student> list = new ArrayList<Student>();
		try {
			session = MyBatisUtil.getSession();
			list = session.selectList("com.liumh.dao.IStudentDao.searchStudentsWhere", student);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

	// 动态查询条件封装到Map对象
	@Override
	public List<Student> searchStudentsMap(Map<String, Object> map) {
		SqlSession session = null;
		List<Student> list = new ArrayList<Student>();
		try {
			session = MyBatisUtil.getSession();
			list = session.selectList("com.liumh.dao.IStudentDao.searchStudentsMap", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	// 动态查询choose
	@Override
	public List<Student> searchStudentsChoose(Student student) {
		SqlSession session = null;
		List<Student> list = new ArrayList<Student>();
		try {
			session = MyBatisUtil.getSession();
			list = session.selectList("com.liumh.dao.IStudentDao.searchStudentsChoose", student);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return list;
	}

}
